package monster.com.cvh.activity;

import android.content.pm.PackageManager;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMWeb;

import butterknife.BindView;
import butterknife.OnClick;
import me.shaohui.bottomdialog.BaseBottomDialog;
import me.shaohui.bottomdialog.BottomDialog;
import monster.com.cvh.R;
import monster.com.cvh.activity.base.PermissionActivity;
import monster.com.cvh.application.TinkerApplicationLike;
import monster.com.cvh.util.StatusBarUtil;

/**
 * 关于界面
 */

public class AboutActivity extends PermissionActivity {
    @BindView(R.id.iv_news_details_title_back)
    ImageView ivNewsDetailsTitleBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.details_title)
    RelativeLayout detailsTitle;
    @BindView(R.id.tv_activity_about_share)
    TextView tvActivityAboutShare;
    @BindView(R.id.tv_activity_about_version_name)
    TextView mTvVersionName;
    ShareAction shareAction = new ShareAction(AboutActivity.this);
    private BaseBottomDialog shareBottomDialog;
    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            //分享开始的回调
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            Log.d("plat", "platform" + platform);
            showToastShort("分享成功啦");
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            showToastShort("分享失败啦");
            if (t != null) {
                Log.d("throw", "throw:" + t.getMessage());
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            showToastShort("分享取消了");
        }
    };
    private UMWeb web;
    private String mVersionName;

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_check;
    }

    @Override
    protected void initEvent() {
        tvTitle.setText("关于我们");
        if (mVersionName != null && !mVersionName.isEmpty()) {
            mTvVersionName.setText(mVersionName);
        }
    }

    @Override
    protected void initData() {
        //修改状态栏颜色
        StatusBarUtil.setStatusBarLightMode(AboutActivity.this, Color.WHITE);
        //资源文件
        web = new UMWeb("http://api.cvh.io/download");
        web.setTitle("简历链");//标题
        web.setDescription("我发现了一款实用的软件");//描述
        try {
            String pkName = TinkerApplicationLike.getContext().getPackageName();
            //本地版本名
            mVersionName = TinkerApplicationLike.getContext().getPackageManager().getPackageInfo(pkName, 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }


    /**
     * 后退
     */
    @OnClick(R.id.iv_news_details_title_back)
    public void onIvNewsDetailsTitleBackClicked() {
        finish();
    }

    /**
     * 分享
     */
    @OnClick(R.id.tv_activity_about_share)
    public void onTvActivityAboutShareClicked() {
        shareBottomDialog = BottomDialog.create(getSupportFragmentManager())
                .setViewListener(new BottomDialog.ViewListener() {
                    @Override
                    public void bindView(View v) {
                        initView(v);
                    }
                })
                .setLayoutRes(R.layout.dialog_share)
                .setDimAmount(0.3f)            // Dialog window dim amount(can change window background color）, range：0 to 1，default is : 0.2f
                .setCancelOutside(true)     // click the external area whether is closed, default is : true
                .setTag("BottomDialog")     // setting the DialogFragment tag
                .show();

    }

    private void initView(View v) {
        /**
         * 微信分享
         */
        v.findViewById(R.id.wechat_share).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shareAction.setPlatform(SHARE_MEDIA.WEIXIN).withMedia(web).setCallback(umShareListener).share();
            }
        });
        /**
         * qq分享
         */
        v.findViewById(R.id.qq_share).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shareAction.setPlatform(SHARE_MEDIA.QQ).withMedia(web).setCallback(umShareListener).share();
            }
        });
        /**
         * 朋友圈分享
         */
        v.findViewById(R.id.friend_share).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shareAction.setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).withMedia(web).setCallback(umShareListener).share();
            }
        });
    }
}
